<?php
// manage.php --- Time-stamp: <2009-01-11 21:57:14 root>
// Copyright 2008 Julian Qian
// Author: lancer@pdn
// Version: $Id: manage.php,v 0.0 2008/12/24 08:04:03 lancer Exp $
// Keywords: 

class Manage extends Controller {

    var $_username;
    var $_uid;
    var $_devids;

    function Manage(){
        parent::Controller();

        $this->_username = $this->session->userdata("username");
        $this->_uid = $this->session->userdata("uid");        
        $this->_devids = $this->session->userdata("devids");

        if(empty($this->_username) ||
           empty($this->_uid) ){
            // echo("No authed!");
            redirect("welcome","refresh");
        }

        $this->load->model("Dev");
        $this->load->model("User");
    }

    function index(){
        $data =  array();
        $this->load->view("header_view");
        $this->load->view("manage_index",$data);
        $this->load->view("footer_view");        
    }

    function defense_index(){
        $this->load->view("header_view");
        $data["devlist"] = $this->_devids;
        $this->load->view("defense/index",$data);
        $this->load->view("footer_view");
    }
    
    function defense(){
        // $data["action"] = site_url("manage/do_defense");
        $devid = $this->input->post("devid");
        $data["status"] = $this->Dev->curr_stat($devid);
        $data["devid"] = $devid;
        $this->load->view("defense/action",$data);
    }

    function do_defense(){
        $defense = $this->input->post("defense");
        $devid = $this->input->post("devid");

        switch($defense){
        case "yes":
            $cmd = "bf";
            $info = "布防";
            break;
        case "no":
            $cmd = "cf";
            $info = "撤防";
            break;
        default:
            echo "请选择具体操作";
            return;
        }
        $run = EHOME_APP."$devid,$cmd";
        @system($run);
        // redirect("result/ok");
        echo $info." 操作成功".$run;
    }

    function status_index(){
        $this->load->view("header_view");
        $data["devlist"] = $this->_devids;
        $this->load->view("status/index",$data);
        $this->load->view("footer_view");        
    }

    function status(){
        $devid = $this->input->post("devid");
        $data["status"] = $this->Dev->get_status($devid);
        
        $this->load->view("status/view",$data);
    }

    function mod_user(){
        $this->load->view("header_view");
        $data["devlist"] = $this->_devids;
        $this->load->view("user/list",$data);
        $this->load->view("footer_view");
    }

    function add_mobile(){
        $devid = $this->input->post("devid");
        $data["devid"] = $devid;
        $this->load->view("user/mobile", $data);
    }

    function delete_mobile(){
        $devid = $this->input->post("devid");
        $mobile = $this->input->post("mobile");
        $this->Dev->delete_mobile($devid, $mobile);
        echo $this->lang->line("delete_mobile_ok");
    }
    
    function new_mobile(){
        $mobile = $this->input->post("mobile");
        $devid = $this->input->post("devid");
        $this->Dev->new_mobile($this->_uid, $devid, $mobile);
    }

    function mod_dev(){
        $devid = $this->input->post("devid");
        $rows = $this->Dev->get_devinfo($devid);
        // $rows = $this->Dev->get_userinfo($this->_uid);
        $data["rows"] = &$rows;
        $wtitles = $this->Dev->warn_titles();
        $titles = array();
        foreach($wtitles as $title){
            $titles[$title] = $this->lang->line($title);
        }
        $data["warnings"] = &$titles;
        
        $this->load->view("user/mod",$data);
    }

    function do_mod(){
        $action = $this->input->post("action");
        $devid = $this->input->post("devid");
        $mobile = $this->input->post("mobile");
        $warn = $this->input->post("warn");
        $warnstr = "";
        switch($action){
        case "active":
            $warnstr = $this->lang->line("default_".$warn);
            break;
        case "deactive":
            $warnstr = "";
            break;
        case "modify":
            $warnstr = $this->input->post("warnval");
        }
        $this->Dev->set_warn($devid, $mobile, $warn, $warnstr);
        echo $warnstr;
    }

    function add_user(){
        $this->load->view("header_view");
        $data["action"] = site_url("manage/do_add_user");
        $this->load->view("user/add", $data);
        $this->load->view("footer_view");
    }

    function do_add_user(){
        $userinfo = array();
        $userinfo["devid"] = $this->input->post("devid");
        $userinfo["username"] = $this->input->post("username");
        $userinfo["password"] = md5($this->input->post("password"));
        if(empty($userinfo["devid"]) ||
           empty($userinfo["username"]) ||
           empty($userinfo["password"])){
            echo "something wrong";
        }
        $this->User->add_user($userinfo);
        echo "增加用户 <strong>".$userinfo["username"]."</strong> 成功";
    }

    function add_dev(){
        $this->load->view("header_view");
        $data["username"] = $this->_username;
        $this->load->view("user/dev", $data);
        $this->load->view("footer_view");
    }

    function do_add_dev(){
        $devid = $this->input->post("devid");
        $username = $this->input->post("username");
        $mobile = $this->input->post("mobile");        
        $ret = $this->Dev->add_dev($devid, $username, $mobile);
        echo $this->lang->line($ret);
    }

    function logout(){
        $this->session->sess_destroy();
        redirect("welcome");
    }
  }

?>